package com.baitiaojun.system.admin.controller.tenant;

import com.baitiaojun.common.constants.HttpStatus;
import com.baitiaojun.common.constants.MsgConstants;
import com.baitiaojun.common.domain.model.result.CommonResult;
import com.baitiaojun.ratelimit.core.annotation.RateLimit;
import com.baitiaojun.repeatsubmit.core.annotation.SubmitRepeat;
import com.baitiaojun.system.admin.controller.tenant.req.TenantRegisterReq;
import com.baitiaojun.system.admin.service.tenant.TenantService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.security.PermitAll;

@Slf4j
@RestController
@Api("租户模块")
@RequestMapping("/admin/tenant")
public class TenantController {

    @Autowired
    private TenantService tenantService;

    @PermitAll
    @SubmitRepeat
    @ApiOperation("创建新租户")
    @PostMapping("/register")
    @RateLimit(key = "tenant", count = 1, time = 5, RATE_LIMIT_TYPE = RateLimit.RateLimitType.IP)
    public CommonResult tenantRegister(@RequestBody @Validated TenantRegisterReq tenantRegisterReq) {
        log.info("===================开始创建新租户===================");
        tenantService.tenantRegister(tenantRegisterReq);
        log.info("===================新租户创建成功===================");
        return CommonResult.ok(HttpStatus.SUCCESS, MsgConstants.CREATE_TENANT_SUCCESS_MSG);
    }
}
